package com.blue.controller;

import com.blue.example.Product;
import com.blue.service.AdminService;
import com.blue.service.ServiceFactory;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by Administrator on 2019/7/29.
 */
public class Leadpage implements Initializable {

    @FXML
    private Button beginInstall;
    @FXML
    private CheckBox accept;
    @FXML
    private Button btnHello;
    @FXML
    private TextField accountField;
    @FXML
    private PasswordField passwordField;

    private AdminService adminService = ServiceFactory.getAdminServiceInstance();

    @FXML
    public void login() throws Exception {
        String account1 = accountField.getText();
        String password1 = passwordField.getText();
        String account = accountField.getText().trim();
        String password = passwordField.getText().trim();
        System.out.println( account1 );
        System.out.println( password1 );
        System.out.println( account );
        System.out.println( password );
        if (account1 != null && !"".equals( account1 )  && password1 != null && !"".equals( password1 )) {
            //调用登录功能
            if (adminService.login( account, password )) {
                Alert alert = new Alert( Alert.AlertType.INFORMATION );
                alert.setTitle( "提示" );
                alert.setContentText( "登录成功!" );
                alert.showAndWait();
                //创建主界面舞台
                Stage mainStage = new Stage();
                //读入布局
                FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "/fxml/main.fxml" ) );
                BorderPane root = fxmlLoader.load();
                Scene scene = new Scene( root );
                scene.getStylesheets().add( "/css/style.css" );
                mainStage.setTitle( "Product Installation System of BLUE" );
                mainStage.setMaximized( true );
                mainStage.setScene( scene );
                mainStage.getIcons().add( new Image( "/img/logo.png" ) );
                mainStage.show();
                //将这个管理员信息传给主控制器
                /*Admin admin = adminService.getAdminByAccount( account );
                Installrocess controller = fxmlLoader.getController();
                controller.setAdmin( admin );*/
                Stage loginStage = (Stage) accountField.getScene().getWindow();
                loginStage.close();

            } else {
                Alert alert = new Alert( Alert.AlertType.INFORMATION );
                alert.setTitle( "提示" );
                alert.setContentText( "账号或密码错误，登录失败!" );
                alert.showAndWait();
            }
        } else {
            Alert alert = new Alert( Alert.AlertType.WARNING );
            alert.setTitle( "提示" );
            alert.setContentText( "账号或密码不能为空，登录失败!" );
            alert.showAndWait();
        }
    }

    @FXML
    protected void acceptProcol() {
        accept.selectedProperty().addListener( (ObservableValue<? extends Boolean> ov,
                                                Boolean old_val, Boolean new_val) -> {
            if (new_val) {
                beginInstall.setDisable( false );
            }
            if (old_val) {
                beginInstall.setDisable( true );
            }

        } );
    }

    @FXML
    public void addProduct(ActionEvent actionEvent) {
        Stage primaryStage = new Stage();
        primaryStage.setTitle( "欢迎使用我的产品" );
        //primaryStage.setScene(new Scene(root, 300, 275));
        Product root = new Product( primaryStage );
        Scene scene = new Scene( root, 950, 500 );
        //scene.getStylesheets().add("/css/style.css");
        //primaryStage.setMaximized(true);
        primaryStage.setScene( scene );
        //固定窗体大小
        primaryStage.sizeToScene();
        primaryStage.setResizable( false );
        primaryStage.show();
        Stage welcomeStage = (Stage) accept.getScene().getWindow();
        welcomeStage.close();

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

}
